// pages/institutions/institutions.js
import loginFn from '../../utils/login.js'
import urls from '../../utils/urls.js'
var app = getApp()
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		location: '',
		latitude: 0,
		longitude: 0,
		schoolId: '', //机构id,编辑时候用
		schoolName: "", //机构名称
		allpicker: '', //所有选项数据
		types: [],
		typeIndex: [0, 0], //默认机构类型
		regionItem: '全部', //地区顶部设置
		regions: ['广东省', '深圳市', '福田区'], //所有地区
		approachaIdx: 0, //默认办学模式 
		approachas: ['民办学校', '公办学校'], //办学形式
		sizesIdx: 0, //默认学校规模
		sizes: [], //学校规模
		majorIdx: 0, //默认专业方向
		major: [], //专业方向
		startTime: '00:00',
		endTime: '00:00',
		primarySize: 'default', //提交按钮样式
		logoimg: '/img/upload.png', //机构logo图片
		logo: 0, //logo图片id
		instImg: [], //机构图片
		schoolImages: [], //机构图片
		tag: [], //机构标签
		tagSelect: [], //选中的标签
		tagshow: false, //显示复选
		tagTxt: '', //机构标签复选显示
		disabled: false,
		plain: false,
		loading: false,
		brandIdx: 0, //默认连锁品牌
		schoolBrand: [], //连锁品牌
		schoolBrandshow: true, //显示连锁品牌
	},
	//提交信息随之页面跳转
	// primary:function(){
	//   wx.navigateTo({
	//     url: '/pages/institucharge/institucharge?tijiao=1',
	//   })
	// },
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		var that = this;
		console.log('options:',options);
		var tag = app.globalData.allselect.tag
		for (var i = 0; i < tag.length; i++) {
			tag[i].checked = false;
		}
		var types = []
		types.push(app.globalData.allselect.schoolType)
		if (app.globalData.allselect.schoolType[0].subType) {
			types.push(app.globalData.allselect.schoolType[0].subType)
		} else {
			types[1] = []
		}
		let schoolId = options.schoolId
		var loginInfo = loginFn()
		loginInfo.schoolId = options.schoolId
		wx.request({
			url: urls.schoolUrl,
			data: loginInfo,
			success: function(res) {
				console.log(res, 'school**************')
				if (res.data.code == '0') {
					that.setData({
						schoolData: res.data.data
					})
				};

			}
		})
		that.setData({
			allpicker: app.globalData.allselect,
			sizes: app.globalData.allselect.schoolScale, //学校规模
			types: types, //机构类别
			major: app.globalData.allselect.subject, //专业方向
			approachas: app.globalData.allselect.schoolSort, //办学形式
			tag: tag, //办学形式
			schoolBrand: app.globalData.schoolBrand //连锁机构

		});


	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	},
	// 函数
	bindTypeChange: function(e) { //选择机构类别
		console.log('picker account 发生选择改变，携带值为', e);
		this.setData({
			typeIndex: e.detail.value
		})
	},
	bindcolumnchange: function(e) { //改变课程类别选项
		console.log(e)
		if (e.detail.column == '0') {
			var types = this.data.types;
			if (this.data.types[0][e.detail.value].subType) {
				types[1] = this.data.types[0][e.detail.value].subType;
			} else {
				types[1] = [];
			}
			this.setData({
				types: types
			})
		};
	},
	chooseinstImg: function(e) { //选择机构图片
		var that = this;
		wx.chooseImage({
			count: 9,
			sizeType: ['compressed'],
			sourceType: ['album', 'camera'],
			success(res) {
				// tempFilePath可以作为img标签的src属性显示图片
				var tempFilePaths = res.tempFilePaths
				for (var i = 0; i < tempFilePaths.length; i++) {
					that.uploadFn(tempFilePaths[i])
				}
			}
		})
	},
	closeimg: function(e) { //删除图片
		console.log(e)
		var insImgs = this.data.instImg
		var schoolImages = this.data.schoolImages
		insImgs.splice(e.currentTarget.dataset.idx, 1)
		schoolImages.splice(e.currentTarget.dataset.idx, 1)
		this.setData({
			instImg: insImgs,
			schoolImages: schoolImages
		})
	},
	uploadFn(tempFilePaths) { //上传图片
		var that = this;
		app.uploadImage(tempFilePaths, (res) => {
			if(res.code == 1) {
				var schoolImages = that.data.schoolImages;
				schoolImages.push(res.data.imageFileId)
				var insImgs = that.data.instImg;
				insImgs.push(tempFilePaths)
				that.setData({
					instImg: insImgs,
					schoolImages: schoolImages
				})
			} else {
				app.alert(res.msg);
			}
		})
	},
	chooselogoImg: function(e) { //选择机构图片
		var that = this;
		wx.chooseImage({
			count: 1,
			sizeType: ['compressed'],
			sourceType: ['album', 'camera'],
			success(res) {
				var tempFilePaths = res.tempFilePaths;
				app.uploadImage(tempFilePaths[0], (res) => {
					if(res.code == 1) {
						that.setData({
							logoimg: tempFilePaths[0],
							logo: res.data.imageFileId
						})
					} else {
						app.alert(res.msg);
					}
				})
			}
		})
	},
	bindRegionChange(e) { //选择地区
		console.log(e)
		this.setData({
			regions: e.detail.value
		})
	},
	approachasChange(e) { //办学形式
		this.setData({
			approachaIdx: e.detail.value
		})
	},
	sizesChange(e) { //学校规模
		if (this.data.sizes[e.detail.value].schoolScale == '连锁') {
			this.setData({
				schoolBrandshow: false
			})
		} else {
			this.setData({
				schoolBrandshow: true
			})
		};
		this.setData({
			sizesIdx: e.detail.value
		})
	},
	majorChange(e) { //专业方向
		this.setData({
			majorIdx: e.detail.value
		})
	},
	startTimeChange: function(e) { //营业开始时间
		this.setData({
			startTime: e.detail.value
		})
	},
	endTimeChange: function(e) { //营业结束时间
		this.setData({
			endTime: e.detail.value
		})
	},

	formSubmit(e) { //表单提交
		console.log('form发生了submit事件，携带数据为：', e.detail.value)
		var that = this;
		this.setData({
			disabled: true
		})
		var alldata = e.detail.value;
		var cansumbit = true;
		for (var key in alldata) {
			if (alldata[key] !== "") {
				if (key == 'schoolTypeId') {
					alldata[key] = app.globalData.allselect.schoolType[this.data.typeIndex[0]].schoolTypeId
					alldata['typeId2'] = app.globalData.allselect.schoolType[this.data.typeIndex[0]].subType[this.data.typeIndex[1]]
						.typeId2
				} else if (key == 'isPrivateSchool') {
					alldata[key] = this.data.approachas[alldata[key]].schoolSortId
				} else if (key == 'schoolScaleId') {
					alldata[key] = this.data.sizes[alldata[key]].schoolScaleId
				} else if (key == 'major') {
					alldata[key] = this.data.major[alldata[key]].subjectId
				} else if (key == 'schoolBrandId') {
					alldata[key] = this.data.schoolBrand[alldata[key]].schoolBrandId
				} else if (key == 'schoolTags') {
					for (var h = 0; h < this.data.tagSelect.length; h++) {
						delete this.data.tagSelect.checked
					}
					alldata[key] = this.data.tagSelect
				};

			} else {
				cansumbit = false;
				console.log(key)
				wx.showToast({
					title: '请把信息补充完整',
					icon: 'none'
				})
				this.setData({
					disabled: false
				})
				break
			}

		}

		if (cansumbit) {
			console.log(this.data.schoolImages.length)
			if (this.data.schoolImages.length == 0) {
				wx.showToast({
					title: '请把信息补充完整',
					icon: 'none'
				})
				this.setData({
					disabled: false
				})
			} else {
				var loginInfo = loginFn();
				for (var key in loginInfo) {
					alldata[key] = loginInfo[key]
				}
				alldata.lat = this.data.latitude
				alldata.lng = this.data.longitude
				alldata.schoolImages = this.data.schoolImages
				alldata.logo = this.data.logo
				alldata.isAdmin = 1
				wx.request({
					url: urls.saveinst,
					method: 'POST',
					data: alldata,
					success: function(e) {
						console.log(e)
						if (e.data.code == '0') {
							wx.showToast({
								title: '保存成功'
							})
							wx.redirectTo({
								url: `/pages/institucharge/institucharge?tijiao=1`,
							})
						} else {
							wx.showToast({
								title: '保存失败',
								icon: 'none'
							})
							that.setData({
								disabled: false
							})
						};
					}
				})
			};

		};

	},
	tagChange: function(e) { //选择标签
		console.log(e)
		this.setData({
			tagshow: true
		})
	},
	featureFn: function(e) { //选择机构标签返回
		console.log(e)
		if (e.detail) {
			var tagTxt = '';
			var tagSelect = [];
			for (var i = 0; i < this.data.tag.length; i++) {
				this.data.tag[i]['checked'] = false
			}
			for (var j = 0; j < e.detail.length; j++) {
				for (var i = 0; i < this.data.tag.length; i++) {
					if (this.data.tag[i].tagId == e.detail[j]) {
						tagSelect.push(this.data.tag[i])
						if (j == e.detail.length - 1) {
							tagTxt += this.data.tag[i].tagName

						} else {
							tagTxt += this.data.tag[i].tagName + '、'

						}
						this.data.tag[i]['checked'] = true
					};
				}
			}
			this.setData({
				tagshow: false,
				tagSelect: tagSelect,
				tagTxt: tagTxt,
				tag: this.data.tag
			})
		} else {
			this.setData({
				tagshow: false
			})
		}

	},
	getlocation: function() { //获取定位
		console.log('获取定位')
		var loginInfos = loginFn()
		console.log(loginInfos)

		var that = this;
		wx.chooseLocation({
			success: function(res) {
				that.setData({
					location: res.name,
					latitude: res.latitude,
					longitude: res.longitude
				})
			}
		})
	},
	schoolBrandChange: function(e) { //连锁机构
		this.setData({
			brandIdx: e.detail.value
		})
	}
})
